package com.kk.xhr.admin.biz.impl;

import com.kk.xhr.admin.biz.IWorkOvertimeBizService;
import com.kk.xhr.admin.controller.request.ApplyQueryRequest;
import com.kk.xhr.admin.controller.request.WorkOvertimeAddRequest;
import com.kk.xhr.admin.controller.vo.WorkOvertimeVo;
import com.kk.xhr.common.ErrorCodeEnum;
import com.kk.xhr.common.exception.BusinessException;
import com.kk.xhr.common.pojo.ApplyStatusEnum;
import com.kk.xhr.model.dto.ApplyQueryRequestDTO;
import com.kk.xhr.model.dto.NotificationDTO;
import com.kk.xhr.model.dto.PageDto;
import com.kk.xhr.model.dto.UserDTO;
import com.kk.xhr.model.dto.WorkOvertimeDTO;
import com.kk.xhr.model.service.INotificationService;
import com.kk.xhr.model.service.IUserService;
import com.kk.xhr.model.service.IWorkOvertimeService;
import com.kk.xhr.util.DateUtil;
import com.kk.xhr.web.vo.PageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 加班biz接口实现
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@Service
@RequiredArgsConstructor
public class WorkOvertimeBizServiceImpl implements IWorkOvertimeBizService {
    private final IWorkOvertimeService workOvertimeService;
    private final IUserService userService;
    private final INotificationService notificationService;

    /**
     * {@inheritDoc}
     */
    @Override
    public PageVO<WorkOvertimeVo> queryPage(Long userId, ApplyQueryRequest request) {
        PageDto<WorkOvertimeDTO> dtos = workOvertimeService.query(ApplyQueryRequestDTO.builder()
            .userId(userId)
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .status(request.getStatus())
            .limit(request.getLimit())
            .offset(request.getOffset())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        UserDTO userDto = userService.queryNormal(userId).orElseThrow(() -> new BusinessException(ErrorCodeEnum.USER_NOT_EXIST));
        String name = userDto.getName();
        String reviewer = userService.queryName(userDto.getBossId());
        return PageVO.fromDTO(dtos, dto -> WorkOvertimeVo.builder()
            .id(dto.getId())
            .name(name)
            .reviewer(reviewer)
            .begin(dto.getBegin())
            .end(dto.getEnd())
            .duration(dto.getDuration())
            .trueBegin(dto.getTrueBegin())
            .trueEnd(dto.getTrueEnd())
            .trueDuration(dto.getTrueDuration())
            .reason(dto.getReason())
            .compensateType(dto.getCompensateType())
            .status(dto.getStatus())
            .type(dto.getType())
            .build());
    }

    /**
     * 分页查询加班申请
     *
     * @param userId  用户id
     * @param request ApplyQueryRequest
     * @return WorkOverTimeVo
     */
    @Override
    public PageVO<WorkOvertimeVo> queryPageCheck(Long userId, ApplyQueryRequest request) {
        PageDto<WorkOvertimeDTO> dtos = workOvertimeService.query(ApplyQueryRequestDTO.builder()
            .reviewerId(userId)
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .status(request.getStatus())
            .limit(request.getLimit())
            .offset(request.getOffset())
            .build());
        if (0 == dtos.getTotal()) {
            return PageVO.empty();
        }
        UserDTO userDto = userService.queryNormal(userId).orElseThrow(() -> new BusinessException(ErrorCodeEnum.USER_NOT_EXIST));
        String name = userDto.getName();
        String reviewer = userService.queryName(userDto.getBossId());
        return PageVO.fromDTO(dtos, dto -> WorkOvertimeVo.builder()
            .id(dto.getId())
            .name(name)
            .reviewer(reviewer)
            .begin(dto.getBegin())
            .end(dto.getEnd())
            .duration(dto.getDuration())
            .trueBegin(dto.getTrueBegin())
            .trueEnd(dto.getTrueEnd())
            .trueDuration(dto.getTrueDuration())
            .reason(dto.getReason())
            .compensateType(dto.getCompensateType())
            .status(dto.getStatus())
            .type(dto.getType())
            .build());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void overtime(Long userId, WorkOvertimeAddRequest request) {
        UserDTO userDto = userService.queryNormal(userId).orElseThrow(() -> new BusinessException(ErrorCodeEnum.USER_NOT_EXIST));
        workOvertimeService.add(WorkOvertimeDTO.builder()
            .userId(userId)
            .reviewerId(userDto.getBossId())
            .begin(DateUtil.validTime(request.getBegin()))
            .end(DateUtil.validTime(request.getEnd()))
            .duration(request.getDuration())
            .compensateType(request.getCompensateType())
            .type(request.getType())
            .status(ApplyStatusEnum.APPLYING)
            .reason(request.getReason())
            .build());
        notificationService.add(NotificationDTO.builder()
            .userId(userDto.getBossId())
            .message("您有待审核的加班申请，请查看")
            .build());
    }

    @Override
    public void modifyStatus(Long id, ApplyStatusEnum status) {
        if (ApplyStatusEnum.REVOKE != status) {
            throw new BusinessException(ErrorCodeEnum.BAD_REQUEST);
        }
        if (!workOvertimeService.modify(id, status)) {
            throw new BusinessException(ErrorCodeEnum.RETRY);
        }
    }
}
